package com.car.action.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.car.action.util.CommonController;
import com.car.common.model.Card;
import com.car.common.model.CircleLike;
import com.car.common.model.Social;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.utils.APIJSON;
import com.utils.PicUtil;
import com.utils.StringUtil;

public class CircleController extends CommonController {
	
	/**
	 * 圈子最新/最热/女生/全部
	 * @param page
	 * @param last_id
	 * @param tags_id
	 * @param type
	 * 
	 */
	public void tags_screen() {
		int page = 1;
        if(!StringUtil.empty(getPara("page"))) {
        	page = getParaToInt("page");
        }
        int last_id = 0;
        if(!StringUtil.empty(getPara("last_id"))) {
        	last_id = getParaToInt("last_id");
        }
        int tags_id = 0;
        if(!StringUtil.empty(getPara("tags_id"))) {
        	last_id = getParaToInt("tags_id");
        }
        
        int type = 4;
        if(!StringUtil.empty(getPara("type"))) {
        	type = getParaToInt("type");
        }
        
        Long user_id = super.getUserId();
        
        ArrayList<Map<String,Object>> result = Card.dao.sel_circle(user_id ,type, page, tags_id, last_id);
        renderJson(APIJSON.sendJSON(result));
	}
	
	/**
     * 圈子详情
     */
    public void circle_detail(){
    	Social social = Social.dao.findFirst("select id,pic,title,sort,status,create_time,desc,notice from social where id = "+ getPara("circle_id"));
    	Map<String,Object> temp = Maps.newHashMap();
    	if (social != null) {
    		temp.put("id",      	social.get("id"));
    		temp.put("pic",     	PicUtil.QINIU_DIR + social.get("pic"));
    		temp.put("title",   	social.get("title"));
    		temp.put("sort",    	social.get("sort"));
    		temp.put("create_time", social.get("create_time"));
    		temp.put("desc",        social.get("desc"));
    		temp.put("notice",      social.get("notice"));
    		Long user_id = super.getUserId();
    		CircleLike like = CircleLike.dao.findFirst("select * from card_like where circle_id = " + getPara("circle_id") + " and user_id = " + user_id );
    		if (like == null) {
    			temp.put("is_focus",    0);
    		} else {
    			temp.put("is_focus",    like.getStatus());
    		}
    		CircleLike likeCnt =  CircleLike.dao.findFirst("select count(id) as cnt from card_like where status = 0 and circle_id = "+ getPara("circle_id"));
    		temp.put("all_like_num",    likeCnt.get("cnt"));
    	}
    	renderJson(APIJSON.sendJSON(temp));
    }
    
    
    /**
     * 关注圈子的人 
     * @param circle_id
     * @param type
     */
    public void circle_focus(){
    	List<CircleLike> circleLikeList = CircleLike.dao.find("select b.user_header,a.user_id,b.nick_name from card_like as a left join users b on b.id = a.user_id where a.status =1 and a.circle_id = " + getPara("circle_id"));
    	ArrayList<Map<String,Object>> result = Lists.newArrayList();
    	for(CircleLike one : circleLikeList) {
    		Map<String,Object> temp = Maps.newHashMap();
    		temp.put("user_header",   PicUtil.QINIU_DIR + one.getStr("user_header"));
    		temp.put("user_id",       one.get("user_id"));
    		temp.put("nick_name",     StringUtil.decode(one.getStr("nick_name")));
    		result.add(temp);
    	}
    	renderJson(APIJSON.sendJSON(result));
    }
    
    /**
     * 圈子列表
     */
    public void circle_list(){
    	List<Social> socialList = Social.dao.find("select * from social  where status = 1 order by sort desc,id desc");
    	
    	for(Social one : socialList) {
    		one.setPic(PicUtil.QINIU_DIR + one.getPic());
    	}		
    	renderJson(APIJSON.sendJSON(socialList));
    }
    
}
